//
//  DataPraseHelper.swift
//  WangPu
//
//  Created by xiaozhi on 15/9/23.
//  Copyright (c) 2015年 zhixiao. All rights reserved.
//

import UIKit
extension NSDictionary{
    func parseDataToGoodDetailItem()->GoodDetailItem{
        var items:GoodDetailItem
        items.cn_price = self["cn_price"] as? String
        items.countryOfManufacture = self["countryOfManufacture"] as? String
        items.finalPrice = self["finalPrice"] as? String
        items.id = self["id"] as? String
        items.image = self["image"] as? String
        items.inWishList = self["inWishList"] as? String
        items.isSaleable = self["isSaleable"] as? String
        items.maxSaleQty = self["maxSaleQty"] as? String
        items.minSaleQty = self["minSaleQty"] as? String
        items.name = self["name"] as? String
        items.deliverytime = self["deliverytime"] as? String
        
        items.price = self["price"] as? String
        items.short_description = self["short_description"] as? String
        items.short_title = self["short_title"] as? String
        items.tierPrice = self["tierPrice"] as? String
        items.rating_avg = self["rating_avg"] as? String
        items.review_total = self["review_total"] as? String
        items.description = self["description"] as? String
        items.first_commit = self["first_commit"] as? NSDictionary
        items.litterImage = self["litterImage"] as? NSArray
        return items
    }
    func parseDataToCartItem()->cartListItem{
        var items:cartListItem
        items.finalPrice = self["finalPrice"] as? String
        items.id = self["id"] as? String
        items.image = self["image"] as? String
        items.maxSaleQty = self["maxSaleQty"] as? String
        items.minSaleQty = self["minSaleQty"] as? String
        items.name = self["name"] as? String
        items.manufacturer = self["manufacturer"] as? String
        items.price = self["price"] as? String
        items.qty = self["qty"] as? String
        items.pid = self["pid"] as? String
        return items
    }
    func parseDataToCommentItem()->CommentObj{
        var items:CommentObj
        items.review_id = self["review_id"] as? String
        items.created_at = self["created_at"] as? String
        items.nickname = self["nickname"] as? String
        items.customer_id = self["customer_id"] as? String
        items.detail = self["detail"] as? String
        items.start_avg = self["start_avg"] as? String
        return items
    }
    func pareseDataToGoodListArray()->[GoodDetailItem]?{
            if let array = self["data"]?["productItems"] as? NSArray{
                var dataArray:[GoodDetailItem] = []
                for dic in array{
                    let adic = dic  as! NSDictionary
                    dataArray.append(adic.parseDataToGoodDetailItem())
                }
                return dataArray
            }
        return nil
    }
    func pareseDataToGoodItems()->GoodDetailItem?{
        if let dic = self["data"] as? NSDictionary{
            return dic.parseDataToGoodDetailItem()
        }
        return nil
    }
    func pareseDataToCartListArray()->[cartListItem]?{
        if let array = self["data"]?["items"] as? NSArray{
            var dataArray:[cartListItem] = []
            for dic in array{
                let adic = dic  as! NSDictionary
                dataArray.append(adic.parseDataToCartItem())
            }
            return dataArray
        }
        return nil
    }
    func parseDataToOrderItemObj()->orderItemObj{
        var items:orderItemObj
        items.id = self["id"] as? String
        items.qty = self["qty"] as? String
        items.image = self["image"] as? String
        items.price = self["price"] as? String
        items.name = self["name"] as? String
        items.type = self["type"] as? String
        items.weight = self["weight"] as? String
        return items
    }
    func parseDataToBannerObj()->BannerObj{
        var items:BannerObj
        items.bannerimage = self["bannerimage"] as? String
        items.id = self["id"] as? String
        items.imgurl = self["imgurl"] as? String
        items.link = self["link"] as? String
        items.store_id = self["store_id"] as? String
        items.title = self["title"] as? String
        return items
    }
    func parseDataToFavoriteObj()->FavoriteObj{
        var items:FavoriteObj
        items.finalPrice = self["finalPrice"] as? String
        items.id = self["id"] as? String
        items.image = self["image"] as? String
        items.isSaleable = self["isSaleable"] as? String
        items.item_id = self["item_id"] as? String
        items.name = self["name"] as? String
        items.price = self["price"] as? String
        return items
    }
    func parseDataToDistrictObj()->DistrictObj{
        var items:DistrictObj
        items.code = self["code"] as? String
        items.name = self["name"] as? String
        items.zip_code = self["zip_code"] as? String
        return items
    }
    func parseDataToCityObj()->CityObj{
        var items:CityObj
        items.code = self["code"] as? String
        items.name = self["name"] as? String
        items.zip_code = self["zip_code"] as? String
        items.phone_code = self["phone_code"] as? String
        if let dataArray = self["sub"] as? [NSDictionary]{
            var array:[DistrictObj]? = []
            for dic in dataArray{
                let item = dic.parseDataToDistrictObj()
                array?.append(item)
            }
            items.sub = array
        }
        return items
    }
    func parseDataToProviceObj()->ProviceObj{
        var items:ProviceObj
        items.code = self["code"] as? String
        items.name = self["name"] as? String
        items.level = self["level"] as? NSNumber
        if let dataArray = self["sub"] as? [NSDictionary]{
            var array:[CityObj]? = []
            for dic in dataArray{
                let item = dic.parseDataToCityObj()
                array?.append(item)
            }
            items.sub = array
        }
        return items
    }
    func parseDataToOrderListItemObj()->orderListItemObj{
        var items:orderListItemObj
        items.created_time = self["created_time"] as? String
        items.increment_id = self["increment_id"] as? String
        items.item_count = self["item_count"] as? String
        items.status = self["status"] as? String
        items.statusLabel = self["statusLabel"] as? String
        items.total = self["total"] as? String
        items.shipping_amount = self["shipping_amount"] as? String
        if let dataArray = self["items"] as? [NSDictionary]{
            var array:[orderItemObj]? = []
            for dic in dataArray{
                let item = dic.parseDataToOrderItemObj()
                array?.append(item)
            }
            items.items = array
        }
        return items
    }
    func pareseDataToOrderListArray()->[orderListItemObj]?{
        if let array = self["data"]?["order_data"] as? NSArray{
            var dataArray:[orderListItemObj] = []
            for dic in array{
                let adic = dic  as! NSDictionary
                dataArray.append(adic.parseDataToOrderListItemObj())
            }
            return dataArray
        }
        return nil
    }
    func pareseDataToOrderCheckOutItem()->orderCheckOutItem?{
        if let dic = self["data"] as? NSDictionary{
            var obj:orderCheckOutItem
            obj.address = dic["address"] as? NSDictionary
            obj.is_out_country = dic["is_out_country"] as? String
            obj.real = dic["real"] as? NSDictionary
            var itemArray:[cartListItem] = []
            if let array = dic["items"] as? NSArray{
                for dic in array{
                    let adic = dic  as! NSDictionary
                    itemArray.append(adic.parseDataToCartItem())
                }
            }
            obj.balance = dic["balance"] as? String
            obj.items = itemArray
            obj.payment = dic["payment"] as? NSDictionary
            obj.base_total = dic["base_total"] as? String
            obj.totals = dic["totals"] as? [NSDictionary]
            obj.base_total = dic["base_total"] as? String
            obj.total = dic["total"] as? String
            obj.shipping = dic["shipping"] as? NSDictionary
            obj.is_authentication = dic["is_authentication"] as? String
            obj.is_out_country = dic["is_out_country"] as? String
            return obj
        }
        return nil
    }
    func getPayWayReturn()->PayObjItems?{
        if let adic = self["data"] as? NSDictionary{
            var payObj:PayObjItems
            payObj.payData = adic
            return payObj
        }
        return nil
    }
    func pareseDataToAddressItem()->AddressListItem{
        var item:AddressListItem
        item.cn_city = self["cn_city"] as? String
        item.cn_district = self["cn_district"] as? String
        item.cn_province = self["cn_province"] as? String
        item.content = self["content"] as? String
        item.id = self["id"] as? String
        item.postCode = self["postCode"] as? String
        item.selected = self["selected"] as? String
        item.telephone = self["telephone"] as? String
        item.username = self["username"] as? String
        return item
    }
    func pareseDataToAddressListArray()->[AddressListItem]?{
        if let array = self["data"]?["addresses"] as? NSArray{
            var dataArray:[AddressListItem] = []
            for dic in array{
                let adic = dic  as! NSDictionary
                dataArray.append(adic.pareseDataToAddressItem())
            }
            return dataArray
        }
        return nil
    }
    func pareseDataToBannerArray()->[BannerObj]?{
        if let array = self["data"] as? NSArray{
            var dataArray:[BannerObj] = []
            for dic in array{
                let adic = dic  as! NSDictionary
                dataArray.append(adic.parseDataToBannerObj())
            }
            return dataArray
        }
        return nil
    }
    func pareseDataToFavoriteArray()->[FavoriteObj]?{
        if let array = self["data"]?["items"] as? NSArray{
            var dataArray:[FavoriteObj] = []
            for dic in array{
                let adic = dic  as! NSDictionary
                dataArray.append(adic.parseDataToFavoriteObj())
            }
            return dataArray
        }
        return nil
    }
    func pareseDataToProviceArray()->[ProviceObj]?{
        if let array = self["data"] as? NSArray{
            var dataArray:[ProviceObj] = []
            for dic in array{
                let adic = dic  as! NSDictionary
                dataArray.append(adic.parseDataToProviceObj())
            }
            return dataArray
        }
        return nil
    }
    func pareseDataToHomeCategoryArray()->[homeCategoryObj]?{
        if let array = self["data"] as? NSArray{
            var dataArray:[homeCategoryObj] = []
            for dic in array{
                let adic = dic  as! NSDictionary
                var obj:homeCategoryObj
                obj.name = adic["name"] as? String
                obj.image = adic["image"] as? String
                var rootNameArray:[NSDictionary] = []
                if let nameArray = adic["rootName"] as? NSArray{
                    for nameDic in nameArray{
                        if let realDic = nameDic as? NSDictionary{
                            rootNameArray.append(realDic)
                        }
                    }
                }
                obj.rootName = rootNameArray
                var goodItemArray:[GoodDetailItem] = []
                if let productArray = adic["product"] as? NSArray{
                    for product in productArray{
                        if let realPro = product as? NSDictionary{
                            goodItemArray.append(realPro.parseDataToGoodDetailItem())
                        }
                    }
                }
                obj.product = goodItemArray
                dataArray.append(obj)
            }
            return dataArray
        }
        return nil
    }
    func pareseDataToCommentArray()->[CommentObj]?{
        if let array = self["data"] as? NSArray{
            var dataArray:[CommentObj] = []
            for dic in array{
                if let adic = dic["comment"] as? NSDictionary{
                    dataArray.append(adic.parseDataToCommentItem())
                }
            }
            return dataArray
        }
        return nil
    }
}